import axios from "axios";
import type { AxiosRequestConfig } from "axios";

const instance = axios.create({
  baseURL: "/",
  timeout: 20000,
});
instance.interceptors.request.use(
  (config) => {

    return config;
  },
  (error) => {
    console.log(error);
    return Promise.reject(error);
  }
);
instance.interceptors.response.use(
  (res) => {
    const code = res.data.code;
    if (code === 402) {
      
      sessionStorage.clear();
    }
    return res;
  },
  (error) => {
    console.log(error);
    return Promise.reject(error);
  }
);
const request = <T = any>(config: AxiosRequestConfig): Promise<T> =>
  new Promise((resolve, reject) => {
    instance
      .request<T>(config)
      .then((res) => {
       
        if (res.status === 200) {
          resolve(res.data);
        } else {
          reject(res);
        }
      })
      .catch((e) => {
        reject(e);
      });
  });
export default request;
